package com.yjt.auth.modules.user;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.joda.time.LocalDateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yjt.auth.common.bean.domain.user.MngUser;
import com.yjt.auth.common.dao.MngUserDAO;
import com.yjt.auth.common.util.YJTAuthUtil;
import com.yjt.common.easyui.datagrid.DataGridDTO;
import com.yjt.common.easyui.datagrid.DataGridJsonDTO;
import com.yjt.common.exception.YJTException;

@Service(MngUserService.BEAN_PACKAGE_NAME)
@Transactional(readOnly=true)
public class MngUserService {

	public static final String BEAN_PACKAGE_NAME = "com.yjt.auth.modules.user.MngUserService";
	
	@Resource(name = MngUserDAO.BEAN_PACKAGE_NAME)
	private MngUserDAO mngUserDAO;
	
	@Transactional(isolation=Isolation.DEFAULT, propagation=Propagation.REQUIRED, readOnly=false)
	public Serializable save(MngUser mngUser) {
		String password = mngUser.getPassword();
		String passwordConfirmed = mngUser.getPasswordConfirmed();
		//判断两次密码是否相等
		if(StringUtils.isNotBlank(passwordConfirmed)) {
			if(!password.equals(passwordConfirmed)) {
				return null;
			} else {
				passwordConfirmed = YJTAuthUtil.threeTimesMD5ToPassword(passwordConfirmed);
			}
		}
		MngUser user = new MngUser();
		user.setCreateDate(LocalDateTime.now().toString("yyyyMMdd", Locale.CHINA));
		user.setCreateTime(LocalDateTime.now().toString("HHmmss", Locale.CHINA));
		//密码进行MD5加密
		user.setPassword(passwordConfirmed);
		user.setAdmin("NO");
		user.setValidate(true);
		user.setName(mngUser.getName());
		mngUserDAO.makePersistent(user, false);
		return mngUser.getId();
	}
	
	public MngUser login(MngUser queryModel) throws YJTException {
		String name = queryModel.getName();
		String password = queryModel.getPassword();
		String isAdminStr = queryModel.getAdmin();
		if(StringUtils.isBlank(name)) {
			throw new YJTException("用户名不能为空串");
		}
		if(StringUtils.isBlank(queryModel.getPassword())) {
			throw new YJTException("密码不能为空串");
		}
		MngUser _queryModel = new MngUser();
		_queryModel.setName(name);
		//是否有该用户名的用户，有且仅有一个
		List<MngUser> users = (List<MngUser>) mngUserDAO.getBeansByBean(_queryModel, MatchMode.EXACT);
		if(users == null || users.size() == 0) {
			throw new YJTException("此名称的用户不存在！");
		}
		//目标用户
		MngUser _mngUser = users.get(0);
		if(!_mngUser.getAdmin().equalsIgnoreCase(isAdminStr)) {
			throw new YJTException("角色选择错误，此用户并非管理员角色！");
		}
		if(!_mngUser.getPassword().equalsIgnoreCase(YJTAuthUtil.threeTimesMD5ToPassword(password))) {
			throw new YJTException("密码错误！");
		}
		//设置密码为空，不返回给浏览器
		_mngUser.setPassword(null);
		BeanUtils.copyProperties(_mngUser, queryModel);
		return queryModel;
	}
	
	public DataGridJsonDTO query(DataGridDTO datagrid, MngUser queryModel) {
		return mngUserDAO.getBeansByBeanForPager(queryModel, MatchMode.ANYWHERE, datagrid, true);
	}
	
	public MngUser findById(String id) {
		return (MngUser) mngUserDAO.getBeanById(MngUser.BEAN_PACKAGE_NAME, id, false);
	}
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public boolean update(MngUser mngUser) {
		String userId = mngUser.getId();
		//关联的 user
		MngUser user = (MngUser) mngUserDAO.getBeanById(MngUser.BEAN_PACKAGE_NAME, userId, false);
		if(!user.getName().equals(mngUser.getName())) {
			user.setName(mngUser.getName());
		}
		String password = mngUser.getPassword();
		String passwordConfirmed = mngUser.getPasswordConfirmed();
		//判断两次密码是否相等
		if(StringUtils.isNotBlank(passwordConfirmed)) {
			if(!password.equals(passwordConfirmed)) {
				return false;
			} else {
				passwordConfirmed = YJTAuthUtil.threeTimesMD5ToPassword(passwordConfirmed);
				user.setPassword(passwordConfirmed);
			}
		} 
		//更新员工信息
		mngUserDAO.makePersistent(user, true);
		return true;
	}
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public int delete(String... ids) {
		//删除此员工在关系表里面的数据 
		mngUserDAO.deleteByIDs(MngUser.BEAN_PACKAGE_NAME, ids);
		return ids.length;
	}
}
